package com.yuanqi.hnucshop.controller;

import com.yuanqi.hnucshop.entity.Goods;
import com.yuanqi.hnucshop.service.GoodsService;
import com.yuanqi.hnucshop.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController{

    @Autowired
    GoodsService goodsService;

    @RequestMapping("/addGoods")
    public JsonResult addGoods(Goods goods){
        Boolean aBoolean = goodsService.addGoods(goods);

        if (aBoolean){
            return new JsonResult(OK);
        }
        else return new JsonResult(400,"添加失败");

    }

    @RequestMapping("/selectGoods")
    public JsonResult selectGoods(String goodsId){

        int realId;

        try{
            realId = Integer.parseInt(goodsId);
        }catch (Exception e){
            return new JsonResult(400, "goodsId错误");
        }

        Goods goods = goodsService.selectGoods(realId);

        if (goods == null) return new JsonResult(400,"商品不存在");

        return new JsonResult<Goods>(200,goods);

    }


}
